import _ from 'lodash';
import validator from 'validator';
import utility from 'utility';
import Promise from 'promise';
import util from 'util';
import * as tools from '../common/tools';
import config from '../config';
import * as ResultMsg from '../constrants/ResultMsg';
import { errorJson, successJson } from '../common/response';
import { checkId, checkName, checkDoc, checkDocs, checkTopicOperateLimit } from '../common/check';
import { UserProxy, TopicProxy, ReplyProxy, TopicCollect } from '../proxy';
import { TopicModel, ReplyModel } from '../models';

export {
  detail,
  update,
  toggleStar,
  block,
  users,
  updateRole,
};

async function detail(req, res, next) {
  const appId = res.locals.appId;
  const userId = validator.trim(req.params.uid) || '';

  if (!userId && req.session.user._id && !userId.equals(req.session.user._id)) {
    return errorJson(req, res, { err: '没权限查看' });
  }

  try {
    const doc = await UserProxy.findOneDetailById(appId, userId);
    doc.avatar = UserProxy.getGravatar(doc);
    successJson(req, res, { data: doc });
  } catch (err) {
    next(err);
  }
}

async function update(req, res, next) {
  const appId = res.locals.appId;
  const weixin = validator.trim(req.body.weixin) || '';
  const qq = validator.trim(req.body.qq) || '';
  const location = validator.trim(req.body.location) || '';
  const signature = validator.trim(req.body.signature) || '';
  const avatar = validator.trim(req.body.avatar) || '';

  const data = {
    weixin,
    qq,
    location,
    signature,
    avatar,
  };

  try {
    const doc = await UserProxy.update(appId, req.session.user._id, data);
    req.session.user = doc.toObject({ virtual: true });
    successJson(req, res, { msg: ResultMsg.UPDATE_SUCCESS });
  } catch (err) {
    next(err);
  }
}

async function toggleStar(req, res, next) {
  const appId = res.locals.appId;
  const userId = req.body.userId;
  const isStar = req.body.isStar;

  const data = {
    isStar,
  };

  try {
    await UserProxy.upate(appId, userId, data);
    successJson(req, res, { msg: ResultMsg.UPDATE_SUCCESS });
  } catch (err) {
    next(err);
  }
}

async function block(req, res, next) {
  const appId = res.locals.appId;
  const userId = req.body.userId;
  const action = req.body.action;

  const data = {
    isBlock: action === 'block',
  };

  try {
    await UserProxy.update(appId, userId, data);
    successJson(req, res, { msg: ResultMsg.UPDATE_SUCCESS });
  } catch (err) {
    next(err);
  }
}

async function users(req, res, next) {
  const appId = res.locals.appId;

  try {
    const docs = await UserProxy.find(appId, {}, {});
    successJson(req, res, { data: docs });
  } catch (err) {
    next(err);
  }
}

async function updateRole(req, res, next) {
  const appId = res.locals.appId;
  const userId = req.body.userId || '';
  const role = req.body.role || 'N';

  const data = {
    role,
  };

  try {
    await UserProxy.update(appId, userId, data);
    successJson(req, res, { msg: ResultMsg.UPDATE_SUCCESS });
  } catch (err) {
    next(err);
  }
}
